package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import dao.utils.GenericHibernateDAO;
import domain.Ofertas;

public class OfertasDAOImpl extends GenericHibernateDAO implements OfertasDAO {

	public OfertasDAOImpl(Session s) {
		super(s);
	}

	public boolean borrarOferta(int Id, Ofertas oferta) {
		try {
			Query id = getSession()
					.createQuery(
							"select count(ofer.idOfertas) from Ofertas as ofer where ofer.idOfertas = ?")
					.setInteger(0, Id);
			Long num = (Long) id.uniqueResult();
			if (num == 0) {
				return false;
			} else {
				getSession().delete(oferta);
				return true;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}

	}

	public boolean guardarOferta(Ofertas oferta) {
		try {
			getSession().save(oferta);
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}

	}

	@SuppressWarnings("unchecked")
	public List<Ofertas> findAll() {
		Query q = getSession().createQuery("From Ofertas");
		return q.list();
	}
}
